package com.wang.lottery.infrastucture.repository;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.lottery.domain.activity.model.po.UserTakeActivityCount;
import com.wang.lottery.domain.activity.repository.IUserTakeActivityCountRepository;
import com.wang.lottery.infrastucture.mapper.UserStrategyExportMapper;
import com.wang.lottery.infrastucture.mapper.UserTakeActivityCountMapper;
import com.wang.lottery.infrastucture.mapper.UserTakeActivityMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author wjh
 * @create 2023-11-08 18:28
 * 用户参与活动仓储
 */
@Service
@Slf4j
public class UserTakeActivityCountRepository extends ServiceImpl<UserTakeActivityCountMapper, UserTakeActivityCount> implements IUserTakeActivityCountRepository {
    
    @Resource
    private UserTakeActivityMapper userTakeActivityMapper;
    @Resource
    private UserStrategyExportMapper userStrategyExportMapper;

    /**
     * 扣减个人活动参与次数(修改user_take_activity_count表)
     *
     * @param activityId 活动id
     * @param uId        用户id
     * @param totalCount 用户总的可领取次数
     * @param leftCount  用户剩余领取的次数
     * @return 扣减结果
     */
    @Override
    public boolean subtractionLeftCount(Long activityId, String uId, Integer totalCount, Integer leftCount) {
        LambdaUpdateWrapper<UserTakeActivityCount> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(UserTakeActivityCount::getActivityId,activityId)
                .eq(UserTakeActivityCount::getuId,uId)
                .setSql("left_count = left_count - 1");
        return this.update(updateWrapper);
    }

    

    @Override
    public UserTakeActivityCount queryUserTakeActivityCount(String uId, Long activityId) {
        return baseMapper.queryUserTakeActivityCount(uId,activityId);
    }
    

    
    

}
